//
//  VisitorView.swift
//  weibo-01
//
//  Created by 韩家奇 on 15/11/4.
//  Copyright © 2015年 hanjiaqi. All rights reserved.
//

import UIKit
import SnapKit
// 访客视图的协议


class VisitorView: UIView {

    // MARK: - 设置视图信息
    /// 设置视图信息
    ///
    /// - parameter imageName: 图片名称，首页设置为 nil
    /// - parameter title:     消息文字
    func setupInfo(imageName: String?, title: String) {
        
        messageLabel.text = title
        
        // 如果图片名称为 nil，说明是首页，直接返回
        guard let imgName = imageName else {
            // 播放动画
            startAnim()
            
            return
        }
        
        iconView.image = UIImage(named: imgName)
        // 隐藏小房子
        homeIconView.hidden = true
        // 将遮罩图像移动到底层
        sendSubviewToBack(maskIconView)
    }
    //MARK: - 设置
    private func startAnim() {
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        
        // 用在不断重复的动画上，当动画绑定的图层对应的视图被销毁，动画会自动被销毁
        anim.removedOnCompletion = false
        
        // 添加到图层
        iconView.layer.addAnimation(anim, forKey: nil)
    }
    //纯代码代发视图
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //设置视图信息
    
    // MARK: - 懒加载控件
    /// 图标，使用 image: 构造函数创建的 imageView 默认就是 image 的大小
    private lazy var iconView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_smallicon")
    
    /// 遮罩图像
    private lazy var maskIconView: UIImageView = UIImageView(imageName: "visitordiscover_feed_mask_smallicon")
    /// 小房子
    private lazy var homeIconView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_house")
    /// 消息文字
    private lazy var messageLabel: UILabel = UILabel(title: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜")
    /// 注册按钮
    lazy var registerButton: UIButton = UIButton(title: "注册", color: UIColor.orangeColor(), backimageName: "common_button_white_disable")
    /// 登录按钮
    lazy var loginButton: UIButton = UIButton(title: "登录", color: UIColor.darkGrayColor(), backimageName: "common_button_white_disable")
}
extension VisitorView {
    private func setupUI() {
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        
        
        for v in subviews{
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        //图标
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-60)
        }
        //房子

        homeIconView.snp_makeConstraints { (make) -> Void in
//            make.centerX.equalTo(self.snp_centerX)
//            make.centerY.equalTo(self.snp_centerY)
            make.center.equalTo(iconView.snp_center)
        }
        // 3> 消息文字
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_centerX)
            make.top.equalTo(iconView.snp_bottom)
            make.width.equalTo(224)
            make.height.equalTo(36)
        }
        // 4> 注册按钮

        registerButton.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(messageLabel.snp_left)
            make.top.equalTo(messageLabel.snp_bottom)
            make.width.equalTo(100)
            make.height.equalTo(36)
        }
        // 5> 登录按钮

        loginButton.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(messageLabel.snp_right)
            make.top.equalTo(messageLabel.snp_bottom)
            make.width.equalTo(100)
            make.height.equalTo(36)
        }
        //遮罩图层
//        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[mask]-0-|", options: [], metrics: nil, views: ["mask": maskIconView]))
//        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[mask]-(btnHeight)-[regButton]", options: [], metrics: ["btnHeight": -36], views: ["mask": maskIconView, "regButton": registerButton]))
        maskIconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top)
            make.bottom.equalTo(registerButton.snp_bottom)
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
        }
        // 设置背景颜色 - 灰度图 R = G = B，在 UI 元素中，大多数都使用灰度图，或者纯色图(安全色)
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
      
    }
}